package com.wmh.baseservice.common.utils;


import com.google.zxing.BarcodeFormat;

import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.wmh.baseservice.common.utils.encrypt.RSAUtil;
import org.springframework.stereotype.Component;
import java.io.File;

import java.nio.file.FileSystems;
import java.nio.file.Path;

/**
 * 二维码生成工具
 */
@Component
public class QrCodeUtil {

    /**
     * 普通二维码
     *
     * @param params 二维码携带的参数
     * @param url    保存路径
     * @param name   文件名称
     */
    public static String createParamsQrCode(String params, String url, String name) throws Exception {
        String basePath=PathUtil.BASE_PATH+url+name+".png";
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(RSAUtil.encryptByPublicKey(params), BarcodeFormat.QR_CODE, 500, 500);
        String pathUrl=PathUtil.getFullRealPath() + url + name;
        String filePath = pathUrl + ".png";
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, "png", path);
        return basePath;
    }

}
